//
//  BankName.m
//  gpos_app
//
//  Created by 崔航飞 on 2018/8/19.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "BankName.h"
#import "GPosAPI.h"

#import "SVProgressHUD.h"
#import "XMRegisterViewController.h"
#import "storageModel.h"
#define PROVINCE_COMPONENT  0
#define CITY_COMPONENT      1
#define DISTRICT_COMPONENT  2
#define KScreenWidth  [UIScreen mainScreen].bounds.size.width
#define KScreenHeight  [UIScreen mainScreen].bounds.size.height

@implementation BankName

- (instancetype)init
{
    if (self = [super init]) {
        [self loadData];
        
    }
    return self;
}

- (void)loadData{
    //requestGetBankInfoWithParams
    __weak typeof(self) weakSelf = self;
    self.modelArr = [[NSMutableArray alloc] init];
    [SVProgressHUD showWithStatus:@"加载中"];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
    NSDictionary * params = @{
                              @"" :@""
                              };
    //    NSLog(@"oj---%@",params);
    [GPosAPI requestGetBankInfoWithParams:params andCallback:^(id obj) {
        NSLog(@"储存列表---%@",obj);
        
        if (obj==nil) {
            
//            weakSelf.hud=[WKProgressHUD popMessage:@"请求失败" inView:self duration:1 animated:YES];
            
        }else{
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                
                NSArray * arr = [[NSArray alloc] init];
                arr = [obj objectForKey:@"data"];
                for (NSDictionary * dic in arr) {
                    storageModel *model = [[storageModel alloc] init];
                    model.bankCnfName = [dic objectForKey:@"bankCnfName"];
                    model.bankId = [dic objectForKey:@"bankId"];
                    [weakSelf.modelArr addObject:model];
                }
//                [weakSelf.tableView reloadData];
                [SVProgressHUD dismiss];
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402){
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else{
                
//                weakSelf.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self duration:1 animated:YES];
            }
            
            
        }
        [self initCityPickerView];
    }];
    
}
- (void)initCityPickerView{
    [self getCityData];
    self.frame=[UIScreen mainScreen].bounds;
    self.backgroundColor =  [[UIColor blackColor]colorWithAlphaComponent:0.4];
    //一个选择器
    //    _cityPicker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, KScreenHeight-216, KScreenWidth, 216)];
    _cityPicker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 216)];
    
    _cityPicker.delegate=self;
    _cityPicker.dataSource=self;
    _cityPicker.backgroundColor=[UIColor whiteColor];
    
    
    //一个工具栏
    //    UIView *viewTool=[[UIView alloc] initWithFrame:CGRectMake(0, KScreenHeight-216-44, KScreenWidth, 44)];
    UIView *viewTool=[[UIView alloc] initWithFrame:CGRectMake(0, KScreenHeight-44, KScreenWidth, 44)];
    
    
    UIColor *color=[UIColor colorWithRed:230/256.0 green:230/256.0 blue:230/256.0 alpha:1];
    viewTool.backgroundColor=color;
    [self addSubview:viewTool];
    //一个取消按钮
    UIButton *btnCancel=[[UIButton alloc] initWithFrame:CGRectMake(5, 5, 40, 30)];
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [btnCancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnCancel setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [btnCancel addTarget:self action:@selector(btnCancelAction) forControlEvents:UIControlEventTouchUpInside];
    [viewTool addSubview:btnCancel];
    //一个确定按钮
    UIButton *btnSure=[[UIButton alloc] initWithFrame:CGRectMake(KScreenWidth-45, 5, 40, 30)];
    [btnSure setTitle:@"确定" forState:UIControlStateNormal];
    [btnSure setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnSure setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [btnSure addTarget:self action:@selector(btnSureAction) forControlEvents:UIControlEventTouchUpInside];
    [viewTool addSubview:btnSure];
    //一个显示城市的lab
    storageModel *model=[[storageModel alloc] init];
    NSMutableArray *array=[NSMutableArray new];
    NSMutableArray * oneArr = [NSMutableArray new];
    for (model in self.modelArr)
    {
        [array addObject:model.bankCnfName];
        [oneArr addObject:model.bankId];
    }
    _arrProvince=array;
    _bankIdArr = oneArr;
    _btnSelectShow=[[UILabel alloc] initWithFrame:CGRectMake(50, 5, KScreenWidth-100, 30)];
    _btnSelectShow.text=[_arrProvince firstObject];
    _bankIdOne = [_bankIdArr firstObject];
    _btnSelectShow.textAlignment=1;
    //    [viewTool addSubview:_btnSelectShow];
    //    [self animationWithView:_cityPicker duration:0.5];
    
    [UIView animateWithDuration:0.6 animations:^{
        
        [self.cityPicker setFrame:CGRectMake(0, KScreenHeight-216, KScreenWidth, 216)];
        
        [viewTool setFrame:CGRectMake(0, KScreenHeight-216-44, KScreenWidth, 44)];
        
    } completion:nil];
    [self addSubview:_cityPicker];
    [self showSkyerCityPicker];
}
/*
 - (void)animationWithView:(UIView *)view duration:(CFTimeInterval)duration{
 
 CAKeyframeAnimation * animation;
 animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
 animation.duration = duration;
 animation.removedOnCompletion = NO;
 
 animation.fillMode = kCAFillModeForwards;
 
 NSMutableArray *values = [NSMutableArray array];
 
 [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
 [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
 [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
 [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
 
 
 animation.values = values;
 animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
 
 
 [view.layer addAnimation:animation forKey:nil];
 
 }
 */
#pragma mark - 数据初始化
- (void)getCityData{
    
    storageModel *model=[[storageModel alloc] init];
        NSMutableArray *array=[NSMutableArray new];
         NSMutableArray * oneArr = [NSMutableArray new];
        for (model in self.modelArr)
        {
            [array addObject:model.bankCnfName];
            [oneArr addObject:model.bankId];
        }
    _arrProvince=array;
    _bankIdArr = oneArr;
   
    
}

#pragma mark- 选择器的数据源代理

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_arrProvince count];
    
}


#pragma mark- 选择器的代理方法

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _btnSelectShow.text = [_arrProvince objectAtIndex:row];
    //
    _bankIdOne = [_bankIdArr objectAtIndex:row];
//    NSLog(@"bank---%@",_bankIdOne);
}


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return KScreenWidth;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
    UILabel *myView;
    
    myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, KScreenWidth, 50)];
    myView.textAlignment = NSTextAlignmentCenter;
    myView.text = [_arrProvince objectAtIndex:row] ;
    myView.font = [UIFont systemFontOfSize:18];
    myView.backgroundColor = [UIColor clearColor];
    
    return myView;
}
#pragma mark -block获取选择的城市词典
- (void)cityPickerSetSelectCity{
    
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(backBankName:)]) {
        [self.delegate backBankName:_btnSelectShow.text];
        
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(bankIdName:)]) {
        [self.delegate bankIdName:self.bankIdOne];
    }
}

- (void)btnCancelAction{
    [self disMissCityPicker];
}
- (void)btnSureAction{
    [self cityPickerSetSelectCity];
    [self disMissCityPicker];
}
- (void)showSkyerCityPicker{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.center = [UIApplication sharedApplication].keyWindow.center;
}
-(void)disMissCityPicker{
    [self removeFromSuperview];
}

@end
